import router from './router'

import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

import {
  setSessionItem,
  getSessionItem,
  removeSessionItem,
} from "./utils/sessionStorage";
import { resolutionToken } from "./api/home/h4a";
import store from './store'
import { reqTokenSave } from "./api/home/index";
import { login } from "./utils/H4a";

// import { reqH4a } from "@/api/home/h4a";
NProgress.configure({ showSpinner: false })
function success(token){
  console.log('success',token);
  setSessionItem(token);
  store.commit("homeStatus/changeToken", token);
  resolutionToken(token).then((res) => {
    if (res.code == 200) {
      let flag = res.data.some((item) => {
        return item == 3;
      });
      let refresh = res.data.some((item) => {
        return item == 0;
      });
      if (refresh) {
        
        removeSessionItem();
        return router.push("/visual/401");
       
      

      }
      localStorage.setItem("TokenFlag", JSON.stringify(flag));
      // console.log(res,'权限',flag);
    }
  });
  router.push("/visual/bigscreen/viewer?reportCode=EArchives_first");
  
}
function error(){
  console.log('error');
  
}

router.beforeEach((to, from, next) => {
  // console.log("to", to);
  // console.log("from", from);
  NProgress.start();

  // if (to.path == "/visual" || to.path == "/visual/") {
  //   //权限
  //   // resolutionToken("%E8%BD%A6%E7%AB%99%E6%B5%B7%E5%85%B3").then((res) => {
  //   //   console.log(res);

  //   // });
  //   next("/visual/bigscreen/viewer?reportCode=EArchives_first");
  //   // next("/enterpriseE");
  // } else {
  //   next();
  // }

  if(process.env.NODE_ENV) {

     if (getSessionItem() || store.state.homeStatus.E_token) {
      // console.log('111');
      // 判断本地或者vuex有token，next()
        if (to.path == "/visual" || to.path == "/visual/") {
          next("/visual/bigscreen/viewer?reportCode=EArchives_first");
          // next("/enterpriseE");
        } else {
          next();
        }
      // next();

    } else {
      // h4a
      login(success,error)
      // window.location.href =
      //   "http://10.62.11.69:7007/untitled/?url=http://10.97.126.167:8080/bigscreen/viewer?reportCode=EArchives_first&appid=QYEDA&view=CCIC_VIEW";

      return

    }
    // next()
  }else {
    next("/visual/bigscreen/viewer?reportCode=EArchives_first");

  }
})

router.afterEach((to,from) => {
  // console.log(to,from);
  if (
    to.path.includes("/visual/thirdView") &&
    from.path == "/visual/bigscreen/viewer"
  ) {
    // 强制刷新页面
    window.location.reload();
  }
    NProgress.done();
})

router.onError((error) => {
    const pattern = /Loading chunk (\d)+ failed/g;
    const isChunkLoadFailed = error.message.match(pattern);
    if (isChunkLoadFailed) {
        // 用路由的replace方法，并没有相当于F5刷新页面，失败的js文件并没有从新请求，会导致一直尝试replace页面导致死循环，而用 location.reload 方法，相当于触发F5刷新页面，虽然用户体验上来说会有刷新加载察觉，但不会导致页面卡死及死循环，从而曲线救国解决该问题
        location.reload();
        // const targetPath = $router.history.pending.fullPath;
        // $router.replace(targetPath);
    }
});
